//package com.zhongli.devplatform.controller;
//
//import cn.binarywang.wx.miniapp.api.WxMaService;
//import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
//import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.zhongli.devplatform.po.user.ReadUserPO;
//import com.zhongli.devplatform.security.weixin.WeixinAuthenticationToken;
//import com.zhongli.devplatform.service.imp.wechat.IWeixinRegisterService;
//import com.zhongli.devplatform.service.imp.user.ReadUserService;
//import com.zhongli.devplatform.component.JwtTokenUtil;
//import com.zhongli.devplatform.component.SpringUtil;
//import com.zhongli.devplatform.bean.Res;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.BadCredentialsException;
//import org.springframework.security.authentication.DisabledException;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RestController;
//
//@RestController
//@RequestMapping("/weixin")
//@Slf4j
//public class WerxinAuthenticationController_bak {
//
//
//    @Autowired
//    private WxMaService wxMaService;
//
//    @Autowired
//    private AuthenticationManager authenticationManager;
//    @Autowired
//    private JwtTokenUtil jwtTokenUtil;
//    @Autowired
//    private UserDetailsService userDetailsService;
//
//    @Autowired
//    private ReadUserService userService;
//
//
//    @PostMapping("/ma/login")
//    public Res miniappLogin(@RequestParam("code") String code,@RequestParam("rawData") String rawData,
//                            @RequestParam("signature") String signature, @RequestParam("encrypteData") String encrypteData,
//                            @RequestParam("iv") String iv) throws Exception {
//        WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(code);
//        String weixinId = session.getOpenid();
//        try {
//            WxMaUserInfo wxMaUserInfo = wxMaService.getUserService().getUserInfo(session.getSessionKey(), encrypteData, iv);
//            IWeixinRegisterService weixinRegisterService = SpringUtil.getBean(IWeixinRegisterService.class);
//            weixinRegisterService.register(wxMaUserInfo);
//        } catch (Exception e) {
//            log.error("微信注册失败【" + e.getMessage() + "】");
//        }
//
//        authenticate(weixinId);
//
//        String userName = userService.getOne(new QueryWrapper<ReadUserPO>().select("user_name").eq("weixin_id", weixinId)).getAccountNum();
//        final UserDetails userDetails = userDetailsService
//                .loadUserByUsername(userName);
//
//        final String token = jwtTokenUtil.generateToken(userDetails);
//
//        return new Res(token);
//    }
//
//
//    private void authenticate(String weixinId) throws Exception {
//        try {
//            authenticationManager.authenticate(new WeixinAuthenticationToken(weixinId));
//        } catch (DisabledException e) {
//            throw new Exception("USER_DISABLED", e);
//        } catch (BadCredentialsException e) {
//            throw new Exception("INVALID_CREDENTIALS", e);
//        }
//    }
//
//
//}
